﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.Linq;
using System.DirectoryServices.AccountManagement;
using System.Collections;

namespace ImageUploader.domain
{
    public class DomainController
    {
        public LDAPConnection LDAPConnection
        {
            get;
            private set;
        }


        public DomainController(LDAPConnection ldapConn)
        {
            LDAPConnection = ldapConn;
        }

        public DomainController()
        {
            LDAPConnection = LDAPConnection;
        }

        /*  using (DirectoryEntry de = LDAPConnection.CreateDirectoryEntry())
  {
      List<User> users = new List<User>();
      DirectorySearcher search = new DirectorySearcher(de, "(objectClass=User)");
      search.PropertiesToLoad.Add("sn");  // surname = last name
      search.PropertiesToLoad.Add("givenName");  // given (or first) name
      search.PropertiesToLoad.Add("mail");  // e-mail addresse
      search.PropertiesToLoad.Add("name");
      search.PropertiesToLoad.Add("jpegPhoto");
      return users;
  }*/


        public List<User> GetNonSystemCriticalUsers(List<string> propertiesToLoad)
        {
            using (DirectoryEntry de = LDAPConnection.CreateDirectoryEntryWithDomainName())
            {
              
                List<User> users = new List<User>();
                DirectorySearcher search = new DirectorySearcher(de, "(objectClass=User)");
                if(propertiesToLoad!=null)
                    propertiesToLoad.AddRange(propertiesToLoad);
                foreach (SearchResult sr in search.FindAll())
                {
                    if (!StaticMethods.IsUserCriticalToAD(sr.GetDirectoryEntry()))
                        users.Add(new User(sr));
                }
                return users;
            }
        }

        public List<User> GetUsers()
        {
            List<string> proppertiesToLoad = new List<string>();
            proppertiesToLoad.Add("sn");  // surname = last name
            proppertiesToLoad.Add("givenName");  // given (or first) name
            proppertiesToLoad.Add("mail");  // e-mail addresse
            proppertiesToLoad.Add("name");
            proppertiesToLoad.Add("street");  
            proppertiesToLoad.Add("co");  // country
            proppertiesToLoad.Add("homePhone");  // phone
            proppertiesToLoad.Add("postalCode");
            proppertiesToLoad.Add("postOfficeBox");
            proppertiesToLoad.Add("jpegPhoto");
            return GetUsers(proppertiesToLoad);
        }

        public List<User> GetUsers(List<string> propertiesToLoad)
        {
            using (DirectoryEntry de = LDAPConnection.CreateDirectoryEntryWithDomainName())
            {
                List<User> users = new List<User>();
                DirectorySearcher search = new DirectorySearcher(de, "(objectClass=User)");
                propertiesToLoad.AddRange(propertiesToLoad);
                foreach (SearchResult sr in search.FindAll())
                    users.Add(new User(sr));                    
                return users;
            }
        }


        public void RemoveUser(User u)
        {
            try
            {
                DirectoryEntry de = u.DirectoryEntry;
                de.Parent.Children.Remove(de);
                de.CommitChanges();

               /* DirectoryEntry de = u.DirectoryEntry;
                string distName = de.Properties["distinguishedName"].Value.ToString();
                string selfAndParentFolder = distName.Substring(0, distName.IndexOf(LDAPConnection.Domain) - 1);
                string selfCN = selfAndParentFolder.Substring(0, selfAndParentFolder.IndexOf(','));
                string parentFolder = selfAndParentFolder.Substring(selfAndParentFolder.IndexOf(',') + 1);
                DirectoryEntry deParent = LDAPConnection.CreateDirectoryEntryWithDomainName(parentFolder);
                DirectoryEntry toRemove = deParent.Children.Find(selfCN, "user");
                deParent.Children.Remove(toRemove);
                deParent.CommitChanges();
                 */


            }
            catch (Exception e)
            {
                throw new Exception("Cannot delete that user.");
            }


        }



    }
}
